<template>
  <div>
    <el-dialog
      :model-value="props.dialogVisible"
      title="调整分组"
      width="500px"
      :close-on-click-modal="false"
      :before-close="handleClose">
      <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="100px" @submit.prevent>
        <el-form-item label="项目分组：" prop="project">
          <el-tree-select
            v-model="userForm.project"
            node-key="pid"
            :data="treeData"
            :props="defaultProps"
            :render-after-expand="false"
            check-strictly
            clearable
            placeholder="请选择项目"
            style="width: 100%;"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit(userFormRef)">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="UserGroupDialog">
import { ref, reactive, watch, ComponentInternalInstance, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { projectList } from '@/api/project-manage'
import { userGroupBatchUpdate } from '@/api/user-manage'

const { proxy } = getCurrentInstance() as ComponentInternalInstance

const emit = defineEmits(['submit', 'update:dialogVisible'])
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
  formData: {
    type: Object,
    default: []
  }
})

const defaultProps = {
  children: 'projectTree',
  label: 'projectName'
}
const treeData = ref<any>([])
const userForm = reactive({
  project: '',
})
const userFormRef = ref<FormInstance>()
const userFormRules = reactive<FormRules>({
})

watch(() => props.dialogVisible, async (newVal) => {
  if (newVal) {
    getProjectData()
  }
})

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

// 关闭弹窗
const handleClose = () => {
  resetForm(userFormRef.value)
  proxy?.$emit('update:dialogVisible', false)
}

// 提交表单
const handleSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid, fields) => {
    if (valid) {
      let arr = <any>[]
      props.formData.map((item: any) => {
        arr.push(item.id)
      })
      let params = {
        userIdList: arr,
        projectId: userForm.project
      }
      await userGroupBatchUpdate(params).then((res: any) => {
        if (res.code === 0) {
          ElMessage.success(`编辑成功！`)
          emit('submit')
          handleClose()
        } else {
          ElMessage.error(`${res.msg}`)
        }
      }).catch(err => {
        console.log(err)
      })
    }
  })
}

// 获取项目树表格数据
const getProjectData = async () => {
  await projectList().then((res: any) => {
    if (res.code === 0) {
      treeData.value = [res.data]
    } else {
      ElMessage.error(`${res.msg}`)
    }
  }).catch(err => {
    console.log(err)
  })
}
</script>

<style lang="stylus" scoped>

</style>
